import Taro, { Component } from '@tarojs/taro'
import { View, Button, Input } from '@tarojs/components'
import { connect } from '@tarojs/redux'

import {userAccessTokenAction} from '../../actions/userAction'
import './index.scss'

@connect((store) => {
  return {...store.userReducer}
}, (dispatch) => ({
  onLoginBtn(params){
    dispatch(userAccessTokenAction(params))
  }
}))
class Login extends Component{
  config = {
    navigationBarTitleText: '登录'
  }
  state = {
    token: ''
  }
  changeToken(e){
    this.setState({
      token: e.target.value
    })
  }
  loginBtn(){
    if(this.state.token){
      let params = {
        accesstoken: this.state.token
      }
      this.props.onLoginBtn(params)
      Taro.redirectTo({url: '/pages/user/index'})
    }else{
      Taro.showToast({title: '请输入accesstoken', icon: 'none'})
    }
    
  }
  render(){
    return(
      <View className='login-container'>
        <Input onInput={this.changeToken.bind(this)} className='access-token' placeholder='请输入accesstoken' />
        <Button onClick={this.loginBtn.bind(this)} className='login-btn'>登录</Button>
      </View>
    )
  }
}

export default Login